//
//  HDusergouCarxiangfa.m
//  HDVideoKitDemok
//
//  Created by ^-^ on 2020/11/8.
//

#import "HDusergouCarxiangfa.h"
#import "Macro.h"
#import "HDUkeInfoCenter.h"
#import "HDServicesManager.h"
#import "HDusergouCarxiangfaModel.h"
#import "UKNetworkHelper.h"

#import "HDCustomMyPickerViewController.h"
#import "HDCarSeriesListPickerController.h"
#import "HDCityPikerViewController.h"
#import "HDDealerModel.h"
#import "HDCityBuyCarModel.h"
#import "HDCarSeriesListModel.h"


@interface HDusergouCarxiangfa()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *iphone;
@property (weak, nonatomic) IBOutlet UIButton *close;
@property (weak, nonatomic) IBOutlet UIButton *time;
@property (weak, nonatomic) IBOutlet UIButton *time1;
@property (weak, nonatomic) IBOutlet UIButton *time2;

@property (strong, nonatomic)NSString *provincecode;
@property (strong, nonatomic)NSString *citycode;


@property (strong, nonatomic)NSString *yixianggouche;//意向车型
@property (strong, nonatomic)NSString *gouchedidian;//购车地点
@property (strong, nonatomic)NSString *jiinxiaoshang;//经销商

@property (strong, nonatomic)NSString *model;//意向车型
@property (strong, nonatomic)NSString *kind;//品系

@property (weak, nonatomic) IBOutlet UILabel *goucardiqulabel;
@property (weak, nonatomic) IBOutlet UILabel *yixianggouchelabel;
@property (weak, nonatomic) IBOutlet UILabel *jingxiaoshanglabel;
@property (weak, nonatomic) IBOutlet UILabel *hintLabel;

///购车意向view
@property (weak, nonatomic) IBOutlet UIView *goucheyixiangView;
///购车地点view
@property (weak, nonatomic) IBOutlet UIView *gochedidianView;
///经销商View
@property (weak, nonatomic) IBOutlet UIView *jingxiaoshangView;
///购车意向label
@property (weak, nonatomic) IBOutlet UILabel *goucheyixiangLabel;
///购车地点label
@property (weak, nonatomic) IBOutlet UILabel *gouchedidianLabel;
///经销商Label
@property (weak, nonatomic) IBOutlet UILabel *jingxiaoshangLabel;

@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView;
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView1;
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView2;




@end

@implementation HDusergouCarxiangfa

- (void)awakeFromNib {
    [super awakeFromNib];
    
    CGFloat radius = 15; // 圆角大小
    UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight; // 圆角位置，全部位置
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.contentView.bounds;
    maskLayer.path = path.CGPath;
    self.contentView.layer.mask = maskLayer;
    
    [self.time setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_unselected")] forState:0];
    [self.time setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_selected")] forState:UIControlStateSelected];
    self.time.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    
    [self.time1 setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_unselected")] forState:0];
    [self.time1 setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_selected")] forState:UIControlStateSelected];
    self.time1.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    [self.time2 setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_unselected")] forState:0];
    [self.time2 setImage:[UIImage imageNamed:HDBundleImage(@"paishe/btn_radiobtn_selected")] forState:UIControlStateSelected];
    self.time2.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    [self.close setImage:[UIImage imageNamed:HDBundleImage(@"video/btn_x")] forState:0];
    
    self.arrowImageView.image = [UIImage imageNamed:HDBundleImage(@"paishe/icon_system_arrowline_right")];
    self.arrowImageView1.image = [UIImage imageNamed:HDBundleImage(@"paishe/icon_system_arrowline_right")];
    self.arrowImageView2.image = [UIImage imageNamed:HDBundleImage(@"paishe/icon_system_arrowline_right")];

    self.name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写您的姓名" attributes:@{NSForegroundColorAttributeName: RGBA(57, 60, 67, 0.56)}];
    self.iphone.attributedPlaceholder =  [[NSAttributedString alloc] initWithString:@"请填写您的电话" attributes:@{NSForegroundColorAttributeName: RGBA(57, 60, 67, 0.56)}];
    /**

    __weak typeof(self) weakSelf = self;

    HDUIPickerView *view1 = [[HDUIPickerView alloc]initWithFrame:CGRectZero];
    view1.changeBlock = ^(NSDictionary * _Nullable city, NSDictionary * _Nullable area) {
        
        if ([weakSelf.provincecode isEqualToString:city[@"code"]] && [weakSelf.citycode isEqualToString:area[@"code"]]) {
            return;
        }
        weakSelf.provincecode = city[@"code"];
        weakSelf.citycode = area[@"code"];
        
        self.gouchedidian = [NSString stringWithFormat:@"%@ %@", city[@"name"], area[@"name"]];
        [weakSelf diquxuanze];
    };
    [self addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.goucardiqulabel.mas_right).offset(20);
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self.goucardiqulabel);
        make.height.mas_equalTo(70);
    }];
    
    
    HDUIPickerView1 *view2 = [[HDUIPickerView1 alloc]initWithFrame:CGRectZero];
    view2.changeBlock = ^(NSString * _Nullable city, NSString * _Nullable area) {
        
        weakSelf.model = city;
        weakSelf.kind = area;
    };

    [self addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.yixianggouchelabel.mas_right).offset(20);
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self.yixianggouchelabel);
        make.height.mas_equalTo(70);
    }];
    
    self.view3 = [[HDUIPickerView2 alloc]initWithFrame:CGRectZero];
    self.view3.changeBlock = ^(NSString * _Nullable city, NSString * _Nullable area) {
        weakSelf.jiinxiaoshang = area;
    };
    [self addSubview:self.view3];
    [self.view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.jingxiaoshanglabel.mas_right).offset(20);
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self.jingxiaoshanglabel);
        make.height.mas_equalTo(70);
    }];
     */
    
    ///添加事件
    UITapGestureRecognizer *yixiangGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(yixiangGesClick)];
    [self.goucheyixiangView addGestureRecognizer:yixiangGes];
    
    UITapGestureRecognizer *gochedidianGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gochedidianGesClick)];
    [self.gochedidianView addGestureRecognizer:gochedidianGes];
    
    UITapGestureRecognizer *jinxiaoshangGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jinxiaoshangGesClick)];
    [self.jingxiaoshangView addGestureRecognizer:jinxiaoshangGes];
}

-(void)setTitlealpath:(CGFloat)titlealpath {
    _titlealpath = titlealpath;
    self.hintLabel.alpha = titlealpath;
}


#pragma mark - buttonClick
- (void)yixiangGesClick {
    
    HDCarSeriesListPickerController *carVc = [[HDCarSeriesListPickerController alloc]initWithTitle:@"意向车型"];
    [self.superVc presentViewController:carVc animated:YES completion:nil];
    @WeakObj(self);
    carVc.getPickerValue = ^(HDCarSeriesListModel * _Nonnull carSeries, HDCarSeriesListModel * _Nonnull carType) {
        selfWeak.yixianggouche = [NSString stringWithFormat:@"%@-%@", carSeries.name,carType.name];
        selfWeak.goucheyixiangLabel.text = [NSString stringWithFormat:@"%@ %@", carSeries.name,carType.name];
        selfWeak.model = carType.code;
        selfWeak.kind = carSeries.code;
    };
    
}
- (void)gochedidianGesClick {
    HDCityPikerViewController *cityVc = [[HDCityPikerViewController alloc]initWithTitle:@"购车地点"];
    [self.superVc presentViewController:cityVc animated:cityVc completion:nil];
    @WeakObj(self);
    cityVc.getPickerValue = ^(HDCityBuyCarModel * _Nonnull province, HDCityBuyCarModel * _Nonnull city) {
        selfWeak.provincecode = province.cityID;
        selfWeak.citycode = city.cityID;
        if (selfWeak.citycode == NULL) {
            selfWeak.gouchedidian = [NSString stringWithFormat:@"%@", province.name];
            selfWeak.gouchedidianLabel.text = [NSString stringWithFormat:@"%@", province.name];
        }else{
            selfWeak.gouchedidian = [NSString stringWithFormat:@"%@ %@", province.name, city.name];
            selfWeak.gouchedidianLabel.text = [NSString stringWithFormat:@"%@ %@", province.name, city.name];
        }
    };

}
- (void)jinxiaoshangGesClick {
    [self diquxuanze];
}
- (IBAction)close:(id)sender {
    [self removeFromSuperview];
}
- (IBAction)time:(UIButton *)sender {
    self.time1.selected = NO;
    self.time.selected = NO;
    self.time2.selected = NO;
    
    sender.selected = YES;
}
- (IBAction)tijiao:(UIButton *)sender {
    if (self.name.text.length <= 0) {
        return [SVProgressHUD showToast:@"请输入姓名"];
    }

    if (self.iphone.text.length <= 0) {
        return [SVProgressHUD showToast:@"请输入手机号"];
    }

    if ([self PhoneNumberMobile:self.iphone.text] == NO) {
        return [SVProgressHUD showToast:@"请输入正确手机号"];
    }

    if (self.time.selected == NO && self.time1.selected == NO && self.time2.selected == NO) {
        return [SVProgressHUD showToast:@"请选择时间"];
    }

    if (self.yixianggouche.length == 0) {
        return [SVProgressHUD showToast:@"请选择车型"];
    }

    if (self.gouchedidian.length <= 0) {
        return [SVProgressHUD showToast:@"请选择地区"];
    }

    if (self.jiinxiaoshang.length <= 0) {
        return [SVProgressHUD showToast:@"请选择经销商"];
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"name"] = self.name.text;
    dic[@"tel"] = self.iphone.text;
    if (self.time.selected == YES) {
        dic[@"ownTime"] = @"一个月内";
    }else if (self.time1.selected == YES) {
        dic[@"ownTime"] = @"3个月内";
    }else if (self.time2.selected == YES) {
        dic[@"ownTime"] = @"3个月以上";
    }
    
    dic[@"model"] = self.model;
    dic[@"address"] = self.gouchedidian;
    dic[@"dealer"] = self.jiinxiaoshang;
    dic[@"kind"] = self.kind;
    NSLog(@"self.jiinxiaoshang;==%@",dic);
    @WeakObj(self);
    [SVProgressHUD show];
    [HDServicesManager getzhibodiaodanDataWithResulprovinceId:self.uuid dic:dic block:^(BOOL isSuccess, NSDictionary * _Nullable dic, NSString * _Nullable alertString) {
        [SVProgressHUD dismiss];
        if (isSuccess == YES) {
            !selfWeak.Handler ?: selfWeak.Handler();

            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            [selfWeak removeFromSuperview];
        }else {
            [SVProgressHUD showToast:@"接口失败"];
        }
        
        
    }];
}

-(BOOL)PhoneNumberMobile:(NSString *)mobile{
    if (mobile.length != 11)
    {
        return NO;
    }
    NSString *MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|6[6]|7[0-35-9]|8[0-9]|9[89])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:mobile];
}


-(void)diquxuanze {
    if (self.provincecode.length <= 0) {
        return [SVProgressHUD showToast:@"请选择地区"];
    }
    [SVProgressHUD show];
    @WeakObj(self);
    [HDServicesManager getjingxiaoshangDataWithResulprovinceId:self.provincecode block:^(BOOL isSuccess, NSArray * _Nonnull dataArray, NSString * _Nonnull alertStr) {
        [SVProgressHUD dismiss];
        if (isSuccess) {
            NSMutableArray *dealerNameArray = [NSMutableArray array];
            for (HDDealerModel *model in dataArray) {
                [dealerNameArray addObject:model.dealerName];
            }
            HDCustomMyPickerViewController *vc = [[HDCustomMyPickerViewController alloc]initWithTitle:@"经销商" leftDataArray:dealerNameArray rightData:@[] componenttitle:@"" dataArraytitle:@""];
            [selfWeak.superVc presentViewController:vc animated:NO completion:nil];


            vc.getPickerValue = ^(NSString * _Nonnull compoentString, NSString * _Nonnull titileString) {
                selfWeak.jiinxiaoshang = compoentString;
                selfWeak.jingxiaoshangLabel.text = compoentString;
            };
        }else{
            [SVProgressHUD showToast:@"经销商获取失败!"];
        }
    
        
    }];
}
@end
